package com.controller.generic_controller;

import com.alibaba.fastjson.JSONObject;
import com.pojo.Consumer;
import com.pojo.front.AuthCodeFront;
import com.service.iter.ILoginService;
import com.util.MD5Utils;
import com.util.Res;
import com.zhenzi.sms.ZhenziSmsClient;
import org.mapstruct.Mapping;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.ContextLoader;
import org.springframework.web.context.WebApplicationContext;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.Random;

@RestController
@RequestMapping("login")
public class LoginController {
    /*
     *
     * 登录服务
     * */
    @Autowired
    private ILoginService loginService;

    /**
     * .
     * 去登录
     *
     * @param consumer 用户
     * @param request  请求
     * @return {@link Res}
     */
    @PostMapping
    public Res goLogin(@RequestBody Consumer consumer,
                       final HttpServletRequest request) {
        HttpSession session = request.getSession();
        String consumerPassword = consumer.getConsumerPassword();
        session.setAttribute("consumerPassword",consumerPassword);
        String saltMD5 = MD5Utils.getSaltMD5(consumerPassword);
        consumer.setConsumerPassword(saltMD5);
        Res aNull = loginService.isNull(consumer);
        Consumer data = (Consumer) aNull.getData();

        String id = session.getId();
        ServletContext servletContext = session.getServletContext();
        servletContext.setAttribute(id, data);
        servletContext.setAttribute(data.toString(),session.getId());
        consumer.setConsumerName(consumer.getConsumerName());
        session.setAttribute( "consumer", data);


        return aNull;

    }
    @RequestMapping("sendVerify")
    @ResponseBody
    public Map<String, String> sendVerify(String consumerPhone, HttpServletRequest request){
        Map<String,String> map = new HashMap<String,String>();
        String apiUrl="https://sms_developer.zhenzikj.com";
        String appId="113104";//在控制台的 我的应用
        String appSecret="NjFkMDMxOWYtNWJhYi00NDEzLThiNzMtMTc3YjFhNjFiNTE4";//在控制台的 我的应用
        ZhenziSmsClient client = new ZhenziSmsClient(apiUrl, appId, appSecret);
        Map<String, Object> params = new HashMap<String, Object>();
        params.put("number", consumerPhone);//你要发送的手机号
        params.put("templateId", "11588");//短信模板里的
        String[] templateParams = new String[2];//模板
        int i = new Random().nextInt(9999 - 1000 + 1);
        int sjs=i+1000;
        templateParams[0] = ""+sjs;
        templateParams[1] = "5分钟";
        params.put("templateParams", templateParams);
        HttpSession session = request.getSession();
        session.setAttribute("consumerPhone",consumerPhone);
        session.setAttribute("authCode",templateParams[0]);

        String result = null;
        try {
            result = client.send(params);
        } catch (Exception e) {
            e.printStackTrace();
            map.put("message","发送失败");
            return map;
        }
        AuthCodeFront authCodeFront = JSONObject.parseObject(result,AuthCodeFront.class);
        map.put("message",authCodeFront.getData());
        return map;
    }
    @PostMapping("register")
    public Res Add(@RequestBody Consumer consumer,HttpServletRequest request) {
        String consumerPassword = consumer.getConsumerPassword();
        String saltMD5 = MD5Utils.getSaltMD5(consumerPassword);
        consumer.setConsumerPassword(saltMD5);
        //判断验证码是否正确
        Res res = new Res();
        HttpSession session = request.getSession();
        String authCode =(String) session.getAttribute("authCode");
        System.out.println("------------");
        System.out.println(session.getId());
        System.out.println(authCode);
        System.out.println("------------");
        if (authCode!=consumer.getAuthCode()){
            res.setCode(402);
            res.setMsg("验证码错误，请重新输入");
            return res;
        }else{
             return loginService.Add(consumer);
        }




    }





}
